/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mycompany.delivery.validation;

import com.mycompany.delivery.dto.UserDTO;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 *
 * @author Katarína Hrabovská <katarina.hrabovska1992@gmail.com>
 */
public class UserDTOValidation implements ConstraintValidator<UserDTOConstraint, UserDTO>{
    @Override
    public void initialize(UserDTOConstraint constraintAnnotation) {

    }

    @Override
    public boolean isValid(UserDTO userDTO, ConstraintValidatorContext ctx) {
        //here is the actual validation check
        boolean isValid = true;
        if ((userDTO.getContact()== null) || (userDTO.getLogin()== null)) {
            isValid = false;
        }
  
        //map the error to the price property
        if ( !isValid ) {
            ctx.disableDefaultConstraintViolation();
            ctx.buildConstraintViolationWithTemplate("chyba")
                    .addPropertyNode("name").addConstraintViolation();
        }
        return isValid;
    }
}
